﻿using GlobalData;
using Microsoft.EntityFrameworkCore;
using Model.Core.Entity.Dbcontexts;
using Model.Core.Services;
using Quartz;
using Quartz.Impl;
using Quartz.Spi;
using RobotClient.Controllers;
using RobotClient.Services;
namespace RobotClient
{
    public class StartUp
    {
        public IConfiguration Configuration { get; }

        public StartUp(IConfiguration configuration)
        {
            Configuration = configuration;
        }


        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<PositionMonitorService>();
            services.AddDbContextFactory<DbcontextBase>(options =>
            {
                options.UseMySql(
                     connectionString: Configuration.GetConnectionString("ConnectionString") + ";Minimum Pool Size=5;Maximum Pool Size=20",
                     serverVersion: new MySqlServerVersion(new Version(8, 2, 0)),

                     mySqlOptions =>
                     {
                         mySqlOptions.EnableRetryOnFailure(
                             maxRetryCount: 5,
                             maxRetryDelay: TimeSpan.FromSeconds(5),
                             errorNumbersToAdd: null);

                     })

                     .UseLoggerFactory(LoggerFactory.Create(builder =>
                     {
                         builder.AddConsole().SetMinimumLevel(LogLevel.Warning);
                     }));
            });

            services.AddSingleton<IHostedService, RobotJobControl>();
            services.AddSingleton<RobotJobControl>();
            services.AddHostedService<RobotMqHelperCtrl>();

            // 由DbContextPool自动提供DbContextFactory
            services.AddSingleton<MqTopics>();
            services.AddSingleton<LocalMqClientCtrl>();
            services.AddSingleton<CloudMqClientCtrl>();


            services.AddSingleton<IHttpClientHelper, HttpClientHelper>();
            services.AddSingleton<IWebSocketClient, WebSocketClient>();
            services.AddSingleton<WebSocketHelper>();
            services.AddSingleton<LocalRobotInfo>();

            services.AddSingleton<JxNavClient>();

            services.AddSingleton<DoJob>();
            services.AddSingleton<JackDevCtrl>();
            services.AddSingleton<JobScheduler>();
            services.AddSingleton<PositionMonitorService>();

            services.AddSingleton<IHostedService, TriggerRefreshService>();
            // 以下是修改后的代码
            services.AddQuartz(q =>
            {
                q.UseMicrosoftDependencyInjectionJobFactory();
                q.SchedulerId = "RobotScheduler";
                q.SchedulerName = "Robot Job Scheduler";
            });
            services.AddQuartzHostedService(options => options.WaitForJobsToComplete = true);
            services.AddControllers();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseRouting();
        }
    }
}